package com.cy.ppj.service.mall.impl;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.dao.mapper.mall.BrandMapper;
import com.cy.ppj.dao.mapper.mall.BrandRecommendMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.domian.mall.Brand;
import com.cy.ppj.model.domian.mall.BrandRecommend;
import com.cy.ppj.model.query.mall.BrandRecommendAdminListQuery;
import com.cy.ppj.model.vo.mall.BrandRecommendAdminDetailVO;
import com.cy.ppj.model.vo.mall.BrandRecommendAdminListVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.comm.FileService;
import com.cy.ppj.service.mall.BrandRecommendService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description 品牌推荐 接口实现类
 * @Author tanyilin
 * @Date 2019/9/26 15:56
 */
@Service
public class BrandRecommendServiceImpl extends BaseService implements BrandRecommendService {

    @Autowired
    private BrandRecommendMapper brandRecommendMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private FileService fileService;


    @Override
    public PageBean<BrandRecommendAdminListVO> getBrandRecommendAdminList(BrandRecommendAdminListQuery query) {

        int total = brandRecommendMapper.selectBrandRecommendAdminListCount(query);
        List<BrandRecommendAdminListVO> list = new ArrayList<>();
        if(total > 0) {
            query.startPage();
            list = brandRecommendMapper.selectBrandRecommendAdminList(query);
            list.stream().forEach(b -> {
                b.setStatusText(b.getStatus().getText2());
            });
        }

        return new PageBean<>(list, total);
    }

    @Override
    public BrandRecommendAdminDetailVO getBrandRecommendAdminDetail(Long brandId) {

        Brand dbBrand = brandMapper.selectByPrimaryKey(brandId);
        state(dbBrand != null, "品牌详情数据异常");

        BrandRecommend dbBrandRecommend = new BrandRecommend();
        dbBrandRecommend.setBrandId(brandId);
        dbBrandRecommend = brandRecommendMapper.selectOne(dbBrandRecommend);
        state(dbBrandRecommend != null, "品牌推荐详情数据异常");

        BrandRecommendAdminDetailVO detailVO = new BrandRecommendAdminDetailVO();
        BeanUtils.copyProperties(dbBrand, detailVO);
        BeanUtils.copyProperties(dbBrandRecommend, detailVO);
        detailVO.setBannerSrc(fileService.getFileUrl(dbBrandRecommend.getBannerPath(), Constants.MALL_BUCKET_NAME));

        return detailVO;
    }

    @Override
    @Transactional
    public void addBrandRecommend(BrandRecommend brandRecommend) {

        Date time = new Date();
        brandRecommend.setCreateTime(time);
        brandRecommend.setUpdateTime(time);
        brandRecommendMapper.insertSelective(brandRecommend);
    }

    @Override
    @Transactional
    public void updateBrandRecommend(BrandRecommend brandRecommend) {

        BrandRecommend dbBrandRecommend = new BrandRecommend();
        dbBrandRecommend.setBrandId(brandRecommend.getBrandId());
        dbBrandRecommend = brandRecommendMapper.selectOne(dbBrandRecommend);
        state(dbBrandRecommend != null, "品牌推荐详情数据异常");

        Date time = new Date();
        brandRecommend.setId(dbBrandRecommend.getId());
        brandRecommend.setUpdateTime(time);
        brandRecommendMapper.updateByPrimaryKeySelective(brandRecommend);
    }

    @Override
    @Transactional
    public void deleteBrandRecommend(BrandRecommend brandRecommend) {

        brandRecommendMapper.delete(brandRecommend);
    }


}
